package com.qtu.user_service.controller;

import com.qtu.redis_service.util.RedisUtil;
import com.qtu.user_service.service.SupplyService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
public class SupplyController {

    @Resource
    private SupplyService supplyService;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @ResponseBody
    @PostMapping("login")
    public Object SupplyLogin(String username,String password){
        return supplyService.SupplyLogin(username,password);
    }

    @ResponseBody
    @PostMapping("getEmployee")
    public Map<String,Object> getEmployee(String tel,String token){
       return supplyService.getEmployee(tel,token);
    }

    @ResponseBody
    @PostMapping("outlogin")
    public Map<String,Object> outlogin(String token){
        return supplyService.outlogin(token);
    }

    @ResponseBody
    @PostMapping("updatePassword")
    public Integer updatePassword(String username,String oldPassword, String newPassword, String token){
       return supplyService.updatePassword(username,oldPassword,newPassword,token);
    }

    /**
     * 月款
     * @return
     */
    @ResponseBody
    @PostMapping("getAmount")
    public Map<String, Object> getAmount(String username,String token){
       return supplyService.getInorderlist(username,token);
    }


    @ResponseBody
    @PostMapping("getlogistics")
    public Map<String,Object> getlogistics(String num,String num1,String username, String token){
      return   supplyService.getlogistics(num,num1,username,token);
    }

    @ResponseBody
    @PostMapping("updateOrderStep")
    public Map<String,Object> updateOrderStep(Integer num,String orderId,String token){
        return supplyService.updateOrderStep(num,orderId,token);
    }

    @ResponseBody
    @PostMapping("getGoods")
    public Map<String, Object> getGoods(Integer orderId, String token){
       return supplyService.getGoods(orderId,token);
    }

    /**
     * 上传头像
     * 文件上传主要是利用了流的思想。
     * @return
     */
    @ResponseBody
    @PostMapping("upload")
    public Map ajaxUpload(MultipartFile file,
                          HttpServletRequest request,
                          HttpServletResponse response) throws Exception{
        Map<String, Object> param=new HashMap();
        if(!file.isEmpty()){
            //nginx服务器文件存放路径
            String path="F:\\image";
            //获取文件名称
            String fileName=file.getOriginalFilename();
            if(fileName.endsWith(".png") || fileName.endsWith(".jpg")){
                File files=new File(path,fileName);
                //完成文件上传
                file.transferTo(files);
                param.put("message", "上传成功");
                param.put("path", "http://image.qtusoft.com/"+fileName);
                param.get(path);
            }
            else{
                param.put("message", "只能上传图片类型");
            }
        }
        else{
            param.put("message", "上传失败");
        }
        return param;
    }

    @ResponseBody
    @PostMapping("getEmployees")
    public Map<String, Object> getEmployees(String token){
        return supplyService.getEmployees(token);
    }

    @ResponseBody
    @PostMapping("updateLogisticsId")
    public Integer updateLogisticsId(Integer orderId){
        return supplyService.updateLogisticsId(orderId);
    }
}
